﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	public class XCircleJig : XUiJigBase
	{
		public XCircleJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = XCircleJig.EStage.eNoOper;
		}

		public XYZ PickCenterPoint(string strPrompt = "请确定圆中心点:")
		{
			try
			{
				this.JigStage = XCircleJig.EStage.eCenterPt;
				this.CenterPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.CenterPoint = null;
			}
			this.JigStage = XCircleJig.EStage.eNoOper;
			return this.CenterPoint;
		}

		public double PickCircleRadius(XYZ ptCenter, string strPrompt = "请确定圆上一点：")
		{
			try
			{
				this.JigStage = XCircleJig.EStage.eOnCurve;
				this.CenterPoint = ptCenter;
				this.OnCurvePoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				bool flag = this.m_prompDlg != null && base.CmdEndType == XUiJigBase.ECmdEndType.eEnter;
				if (!flag)
				{
					this.OnCurvePoint = null;
					return 0.0;
				}
				base.CmdEndType = XUiJigBase.ECmdEndType.eOther;
				XYZ xyz = base.Client2Revit(this.m_ptCur);
				string inputValue = this.m_prompDlg.GetInputValue();
				double num = Convert.ToDouble(inputValue) / 304.8;
				XYZ xyz2 = (xyz - this.CenterPoint).Normalize();
				this.OnCurvePoint = this.CenterPoint + xyz2 * num;
			}
			this.JigStage = XCircleJig.EStage.eNoOper;
			return this.CenterPoint.DistanceTo(this.OnCurvePoint);
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			bool flag = this.JigStage == XCircleJig.EStage.eOnCurve;
			if (flag)
			{
				Graphics graphics = paintEventArgs.Graphics;
				System.Drawing.Point point = base.Revit2Client(this.CenterPoint);
				int distance = XDrawable2D.GetDistance(point, this.m_ptCur);
				XCircle2D xcircle2D = new XCircle2D(point, distance);
				XGraphics g = new XGraphics(graphics, base.JigPen);
				xcircle2D.Draw(g);
			}
		}

		public override List<Curve> CreateCurves()
		{
			List<Curve> list = new List<Curve>();
			try
			{
				bool flag = this.CenterPoint != null && this.OnCurvePoint != null;
				if (flag)
				{
					double num = this.CenterPoint.DistanceTo(this.OnCurvePoint);
					Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(base.SketchPlane.GetPlane().Normal, this.CenterPoint);
					Arc arc = Arc.Create(plane, num, 0.0, Math.PI);
					arc.MakeUnbound();
					list.Add(arc);
				}
			}
			catch
			{
			}
			return list;
		}

		public XYZ CenterPoint { get; set; }

		public XYZ OnCurvePoint { get; set; }

		public XCircleJig.EStage JigStage { get; set; }

		public enum EStage
		{
			eNoOper,
			eCenterPt,
			eOnCurve
		}
	}
}
